# views.py

import pdb, itertools
import sys

from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import Http404, HttpResponseRedirect, HttpResponseNotFound, HttpResponse
from django.core.urlresolvers import reverse
from django.forms.models import modelformset_factory
from django.forms.formsets import formset_factory

from psd.RcodeHooks import print_demographics, print_demographics_async, schedule_async, install_packages_async, test_R_async, make_nametags_async
import psd.register.demographics
import psd.register.views.contact
from psd.register.psdcheckbox import genCodeForSeekAndPrefs, genSeekAndPrefs
from psd.register.models import Person, RegRecord, BreakRecord, DateRecord, CruiseRecord, Event, MatchRecord, TableRecord, fetch_regrecord
from psd.register.matchmaker import updateMatchRecords_async
from psd.register.views.printouts import make_schedules
from psd.register.forms import ScheduleForm, NextSheetForm, BreakForm, MultiBreakForm, CruiseForm


from psd.register.views.util import HttpStreamTextResponse

import logging
logger = logging.getLogger('psd.register.dashboard')


class MyAction:
      action = "default"
      action_description = "default action"
      argument_description = "bleh"
      
      def __init__(self, arg1, arg2, arg3):
          self.action=arg1
          self.action_description=arg2
          self.argument_description=arg3
          
ACTIONS = ( MyAction('', 'Clean Display', ''),
        MyAction('editevent', 'Edit the Event Record', ''),
        MyAction('listcomments', 'List Comments, Notes and Referrals', ''),
        MyAction('listchildcare', 'List Childcare Requests', ''),
        
        MyAction('listpeople', 'List People Registered', '[all]: list everyone, not just this event.'),
        MyAction('email', 'Make email list of participants (seperates checked folks vs. not)', ''),
        MyAction('calcpay', 'Calc number of registered folks, total dollars collected, etc.', '' ),
        MyAction('emailpreevent', "Mass information email pre event", ""),
        MyAction('demographics', "Print out demographics of event", 'NotNo/In/All: Who to include from data.' ),
        MyAction('nametags', "Make csv list that can be turned into nametags", "" ),
        "",
        MyAction('matrix', '(1) Make the dating matrix files', '' ),
        MyAction('plandates', "(2) Figure out who is dating whom when and where", ''),
        MyAction('schedules', "(3) Make pdf of everyone's schedules", '' ),
        MyAction('checkin', "(4) Check-in participants", '' ),
        "",
        MyAction('datesheet', "Enter marked up date sheets into database.", '' ),
        MyAction('countdates', "Count the number of dates everyone has", ''),
        MyAction('listmissingsheets', "List datesheets for event that are not filled in", "" ),
        MyAction('listbadcruises', "List cruise records with psdids that do not exist", "" ),
        MyAction('emailpostevent', "Mass email for matches post event", ""),
        "",
        MyAction('maketabletable', "Admin: Make an initial table of the dating tables.  Run once.  Will delete old tables.", 'N = 100: Number of tables' ),
        MyAction('clean', 'Admin: Clean Database (remove Person Records and User accounts without RegRecords)', '' ),
        MyAction('fix', 'Admin: Fix Group Flags - set the "group" flag based on number of people listed on regrecord', ''),
        MyAction('installRpackages', 'Admin: Install SQL package into R program from CRAN repository.', '' ),
        MyAction('testR', 'Admin: Test R Calling.', '' )
        )





@staff_member_required
def check_in_driver(request, event_name, show_all=False):
    
    try:
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do check-in for an event that does not exist or is closed.  Please try again." }, 
                                   context_instance=RequestContext(request)  )

    to_update = []
    to_warn = []
    if request.method == 'POST':
        here_list = request.POST.getlist('here')
        RegRecord.objects.filter(event=event_name, id__in=here_list).update(here=True)

        cancel_list = request.POST.getlist('cancelled')
        RegRecord.objects.filter(id__in=cancel_list).update(cancelled=True)

        id_list = request.POST.getlist('paid')
        for x in id_list:
            if x in here_list or x in cancel_list:
                to_update.append(x)
            else:
                to_warn.append(x)
        RegRecord.objects.filter(id__in=to_update).update(paid=True)

    regs = RegRecord.objects.filter( event=event_name ).order_by('nickname')
    viewable = regs.filter(here__exact=False, cancelled__exact=False)
    warnable = regs.filter(id__in=to_warn)
    return render_to_response('checkin.html', {'regs': viewable, 'warnings': warnable})





@staff_member_required
def check_in(request, event_name ):
    
    try:
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do check-in for an event that does not exist or is closed.  Please try again." }, 
                                   context_instance=RequestContext(request)  )

    to_update = []
    to_warn = []
    if request.method == 'POST':
        here_list = request.POST.getlist('here')
        RegRecord.objects.filter(event=event_name, id__in=here_list).update(here=True)

        cancel_list = request.POST.getlist('cancelled')
        RegRecord.objects.filter(id__in=cancel_list).update(cancelled=True)

        id_list = request.POST.getlist('paid')
        for x in id_list:
            if x in here_list or x in cancel_list:
                to_update.append(x)
            else:
                to_warn.append(x)
        RegRecord.objects.filter(id__in=to_update).update(paid=True)

    regs = RegRecord.objects.filter( event=event_name ).order_by('nickname')
    viewable = regs.filter(here__exact=False, cancelled__exact=False)
    warnable = regs.filter(id__in=to_warn)
    return render_to_response('checkin.html', {'regs': viewable, 'warnings': warnable})




@staff_member_required
def multi_break( request ):
       
    BreakFormset = formset_factory(BreakForm, extra=10, max_num=10)
  
    if request.method == 'POST':
        print "At post"
        pformset = BreakFormset(request.POST)
        rform = MultiBreakForm(request.POST)
        if pformset.is_valid() and rform.is_valid():
            
            rzn = rform.cleaned_data['reason']
            print "General reason: " + str(rzn)
            
            for form in pformset.forms:
                    if not form.cleaned_data:
                        continue
                    pcd = form.cleaned_data
                    p = BreakRecord(**pcd)
                    if p.notes == None or p.notes=="":
                        p.notes = rzn
                    print "Saving ", p
                    p.save()
        #else:
        #    return render_to_response( 'error.html', {'message' : "Sorry.  Forms invalid. Please try again." }, 
        #                           context_instance=RequestContext(request)  )
    else:
        pformset = BreakFormset()
        rform = MultiBreakForm()
        
    return render_to_response('multi_break.html', {'pformset': pformset, 'rform': rform}, context_instance=RequestContext(request))







@staff_member_required
def list_comments(request, event_name, acts):
    """List all comments, notes and referrals made by people registering for specified event.
     Useful for generating who knows whom."""

    regs = RegRecord.objects.filter(event=event_name, cancelled=False).order_by( '-id' )

    return render_to_response('list_comments.html', {'regs': regs, 'event_name':event_name, 'actions':acts})



@staff_member_required
def list_childcare(request, event_name, acts):
    """List all childcare requests made by people registering for specified event.
     Useful for generating who knows whom."""

    regs = RegRecord.objects.filter(event=event_name, cancelled=False).order_by( '-id' )

    return render_to_response('list_childcare.html', {'regs': regs, 'event_name':event_name, 'actions':acts})



@staff_member_required
def list_people(request, event_name, acts, all_people=False):
    """List all people and comments made by people registering for specified event"""

    if all_people:
        regs = RegRecord.objects.all().order_by( '-id' )
    else:
        regs = RegRecord.objects.filter(event=event_name, cancelled=False).order_by( '-id' )
        
    return render_to_response('list_people.html', {'regs': regs, 'event_name':event_name, 'actions':acts})





@staff_member_required
def generate_email_list(request, event_name, acts):
    """Generate mailing list for the event"""

    regs = RegRecord.objects.filter(event=event_name)

    return render_to_response('email_list.html', {'regs': regs, 'event_name':event_name, 'actions':acts})




def clean_database():
    """
    Delete person records with no regrecords.  
    Delete users with no regrecords that are not staff level
    """
    
    results = unicode("")
    users = User.objects.all()
    rrs = RegRecord.objects.all()
    
    all_rr_ids= set()
    allfolk = set()
    results += "\nID List: "
    for rr in rrs:
        folk = [p.psdid for p in rr.people.all() ]
        allfolk.update(folk)
        results += " " + rr.psdid + "/" + str(rr)
        all_rr_ids.add( rr.psdid )
        
    people_deleted = 0
    for p in Person.objects.all():
        if not p.psdid in allfolk:
            res = "Deleting %s" % p 
            logger.debug( res )
            results += "\n" + res
            p.delete()
            people_deleted += 1
            
    users_deleted = 0
    for usr in users:
        if not usr.is_staff:
            if not usr.username in all_rr_ids:
                res = "Deleting User %s" % usr
                logger.debug( res )
                results += "\n" + res
                usr.delete()
                users_deleted += 1 
        else:
            results += "\nSkipping staff " + str(usr)
         
    return """<pre>database cleaned
    # People deleted = %s
    # users deleted = %s
    Details: %s
    </pre><br>
    All rrs: %s""" % (people_deleted, users_deleted, results, all_rr_ids )


def fix_group_codes():
    res = ""
    for rr in RegRecord.objects.all():
        if (rr.is_group and len( rr.people.all() ) == 1) or (not rr.is_group and len(rr.people.all()) != 1):
            st = "%s  %s  %s  %s" % ( rr.psdid, rr.event, rr.is_group, len(rr.people.all()))
            logger.debug( "Fix Group Code: " + st )
            res += "\n<br>" + st
            if rr.is_group:
                 rr.is_group = False
            else:
                 rr.is_group = True
            rr.save()
    if res == "":
        res = "no broken group flags found"
        
    res2 = ""
    for pp in Person.objects.all():
        seeks = pp.seek_gender_set
        prefs = pp.pref_gender_set
        if seeks == prefs:
            pp.seek_gender = genCodeForSeekAndPrefs( seeks, set() )
            pp.save()
            res2 += "\n<br>Updated seek-pref set for %s to %s" % (pp, pp.seek_gender)
    if res2 == "":
        res2 = "no bad seek-pref sets found"
        
    return res + "\n<br>" + res2



def list_bad_cruises_iter( event_name ):
    
    dr = CruiseRecord.objects.filter( event=event_name ).order_by( 'psdid' )
    
    yield( """List of bad cruise records (and all other records by the same person).  Bad cruise records have been removed from the database.<table>""")
    for d in dr:
        rr = fetch_regrecord( event_name, d.other_psdid )
        if rr == None:
            d.delete()
            yield "<tr><td>%s<td><td>" % (d,)
            for xx in CruiseRecord.objects.filter( event=event_name, psdid=d.psdid ):
                yield str(xx.other_psdid) + " " 
            yield "</tr>"
            
    yield( "</table>" )



def list_missing_datesheets_iter( event_name ):
    
    dr = DateRecord.objects.filter( event=event_name ).order_by( 'psdid' )
    
    yield( """<table>""")
    for d in dr:
        if d.said_yes == None:
            yield "<tr><td>%s<td></tr>\n" % (d,)
        
    yield "<tr><td>Next block<hr></td></tr>"
    
    dr = dr.order_by( 'other_psdid' )
    for d in dr:
        if d.they_said_yes == None:
            yield "<tr><td>%s<td></tr>\n" % (d,)

    yield( "</table>" )

    

@staff_member_required   
def schedule_form(request, event_name):
    evt = Event.objects.get(event=event_name)
    if request.method == 'POST':
        form = ScheduleForm(request.POST)
        if form.is_valid(): 
            trials = form.cleaned_data["trials"]
            rounds = form.cleaned_data["rounds"]
            who_incl = form.cleaned_data["include"]
        
            return HttpStreamTextResponse( schedule_async( event_name, rounds, trials, who_include=who_incl), event_name, ACTIONS=ACTIONS )
        
    form = ScheduleForm()
    return render_to_response('schedule_form.html', {'form': form, 'event': event_name}, context_instance=RequestContext(request) )



@staff_member_required
def potential_matches( request, event_name, psdid ):
    try:
        peep = RegRecord.objects.get( event=event_name, psdid=psdid )
    except:
         return HttpResponseNotFound('<h1>event %s or psdid  %s not real?</h1>' % (event_name, psdid, ) )

    recs = MatchRecord.objects.filter( psdid1=psdid, event=event_name )
    recs2 = MatchRecord.objects.filter( psdid2=psdid, event=event_name )
 
    likes = dict( [(r.psdid2, r) for r in recs ] )
    likeds = dict( [(r.psdid1, r) for r in recs2 ] )
    
    alls = set(likes.keys()).union( likeds.keys() )
    matchlist = []
    
    mutuals = set(likes.keys()).intersection( likeds.keys() )
    crushes = set(likes.keys()).difference( likeds.keys()  )
    stalkers = set(likeds.keys()).difference( likes.keys() )
    
    for psdid2 in itertools.chain( mutuals, crushes, stalkers ):
        if likes.has_key(psdid2):
            lh = likes[psdid2]
        else:
            lh = None
        if likeds.has_key(psdid2):
            rh = likeds[psdid2]
        else:
            rh = None

        yay = not( lh == None or rh == None )

        try:
            person2 = RegRecord.objects.get( event=event_name, psdid=psdid2 )
        except:
          person2 = 'psdid %s for %s not found in reg records?</h1>' % (psdid2, event_name)

        matchlist.append( {'psdid2':psdid2, 'like':lh, 'liked':rh, 'person2':person2, 'yay':yay } )
    
    return render_to_response( 'potential_matches.html', locals(),
                                 context_instance=RequestContext(request)  )
    
 
 
@staff_member_required
def break_matches(request, event_name, psdid ):
    """
    Break matches.  Remove from the current potential matches matrix and
    also add a BreakRecord so that there is never a match in the future,
    either.
    """
    try:
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do break list for an event '%s' that does not exist.  Please try again." % (event_name,)}, 
                                   context_instance=RequestContext(request)  )

    try:
        rr = RegRecord.objects.get(psdid=psdid, event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do break list for a PSDID '%s' that does not exist.  Please try again." % (psdid,) }, 
                                   context_instance=RequestContext(request)  )

    break_list = []
    if request.method == 'POST':
        break_list = request.POST.getlist('break')
        brecs = MatchRecord.objects.filter(event=event_name, psdid1=psdid, psdid2__in=break_list)
        for br in brecs:
            mt = BreakRecord(psdid=psdid, other_psdid=br.psdid2, notes="hand break via break_matches function")
            mt.save()
            br.delete()

    liked = set( [r.psdid1 for r in MatchRecord.objects.filter( event=event_name, psdid2=psdid ) ])
    matches = MatchRecord.objects.filter( event=event_name, psdid1=psdid ).order_by('psdid2')
    
    for m in matches:
        try:
            p2 = RegRecord.objects.get( psdid=m.psdid2, event=event_name )
            m.namestring = p2.namestring
            m.mutual = m.psdid2 in liked
        except:
            logger.error( "Failed to find the RegRecord for '%s-%s'" % ( event_name, m.psdid2 ) )
            m.namestring = "[Error: Failed to Find]"
            
    return render_to_response('matchlist.html', {'matches': matches, 'rr': rr, 'breaklist':break_list})



def gen_next_date_sheet_form( request, event_name, just_submitted=False, submitted_psdid=None, dates=None, err_message=None ):
    form = NextSheetForm()
    nextids = list(r.psdid for r in RegRecord.objects.filter( event=event_name, cancelled=False ) )
    nextids.sort()
    return render_to_response( "next_sheet_form.html", {'form':form, 'event':event_name, 'just_submitted':True, 'dates':dates,
                                                 'submitted_psdid':submitted_psdid, 'err_message':err_message, 'nextids':nextids }, 
                                                 context_instance=RequestContext(request) )
    
    
@staff_member_required   
def next_date_sheet(request, event_name):
    evt = Event.objects.get(event=event_name)
    if request.method == 'POST':
        form = NextSheetForm(request.POST)
        if form.is_valid(): 
            psdid = form.cleaned_data["psdid"].upper()
            rev = reverse("date-sheet", kwargs={'event_name':event_name,'psdid':psdid} )
            return HttpResponseRedirect( rev )

    else:
        return gen_next_date_sheet_form( request, event_name )
    

@staff_member_required
def date_sheet(request, event_name, psdid, pretty=False ):
    
    try:
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do date list for an event '%s' that does not exist or is closed.  Please try again." % (event_name,)}, 
                                   context_instance=RequestContext(request)  )

    try:
        rr = RegRecord.objects.get(psdid=psdid, event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to do date list for a PSDID '%s' that does not exist or is closed.  Please try again." % (psdid,) }, 
                                   context_instance=RequestContext(request)  )

    #CruiseFormset = modelformset_factory(CruiseRecord, exclude=('psdid','event_name',), extra=3, max_num=5)
    CruiseFormset = formset_factory(CruiseForm, extra=3, max_num=5)

    if request.method == 'POST':
        cformset = CruiseFormset(request.POST)
      
        if cformset.is_valid():
            err_message = ""
            for form in cformset.forms:
                if not form.cleaned_data:
                    continue
                #pcd = form.cleaned_data
                #p = CruiseRecord(**pcd)
                #p.psdid = psdid
                #p.event = event_name
                opsdid = form.cleaned_data['other_psdid'].upper()
                if RegRecord.objects.filter(psdid=opsdid, event=event_name).count() == 0:
                    err_message = err_message + "<br>Bad Cruise - '%s'" % (opsdid,)
                    
                if CruiseRecord.objects.filter(psdid=psdid,event=event_name,other_psdid=opsdid).count() == 0:
                        p = CruiseRecord( psdid=psdid, event=event_name, other_psdid=opsdid)
                        print "Saving ", p    
                        p.save()
                else:
                    print "cruise record already exists", opsdid

            
            yeses = request.POST.getlist('yes')                    
            dates = DateRecord.objects.filter( event=event_name, psdid=psdid ).order_by('round')
            for d in dates:
                d2 = DateRecord.objects.get(psdid=d.other_psdid, event=event_name, other_psdid=psdid)
                if not (d.other_psdid in yeses):
                    d.said_yes = False
                    d2.they_said_yes = False
                else:
                    d.said_yes = True
                    d2.they_said_yes = True
                d.save()
                d2.save()
                    
            return gen_next_date_sheet_form( request, event_name, just_submitted=True, submitted_psdid=psdid, dates=dates, err_message=err_message )
    else:
        queryset=CruiseRecord.objects.filter(psdid=psdid,event=event_name)
        cformset = CruiseFormset(initial=queryset.values())
        
          
    dates = DateRecord.objects.filter( event=event_name, psdid=psdid ).order_by('round')

#    gots = [x.round for x in dates]
#    mx = max( gots )
#    miss = set( range(1,mx+1)).difference(gots)
    
#    newdates = []
#    for r in range(1,mx+1):
          
    for d in dates:
        d.other_person = fetch_regrecord( event_name, d.other_psdid )
        if not d.other_person == None:
            d.other_code = d.other_person.miniCode()


    if pretty:
#          rnds = set( [x.round for x in dates] )
#          maxrnds = max( rns )
#          for blk in rnds - range(1,maxrnds):
#                d.
          return render_to_response('prettydatesheet.html', {'dates': dates, 'rr': rr, 'event':event, 'cformset':cformset } )
    else:
          return render_to_response('datesheet.html', {'dates': dates, 'rr': rr, 'event':event, 'cformset':cformset } )

    

   


@staff_member_required
def view_user( request, event_name, psdid ):
    try:
        rr = RegRecord.objects.get(event=event_name, psdid=psdid)
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to edit a regrecord (or event) that does not exist  Please try again." }, 
                                   context_instance=RequestContext(request)  )
    
    recs = MatchRecord.objects.filter( psdid1=psdid, event=event_name )
 
    for r in recs:
        try:
            r.person2 = RegRecord.objects.get( event=event_name, psdid=r.psdid2 )
        except:
          r.person2 = '<h1>psdid %s for %s not found in reg records?</h1>' % (r.psdid2, event_name)
           
    match_text = psd.register.views.contact.get_match_text( event, rr, "", True )
    return render_to_response('user_blurb.html', locals(), context_instance=RequestContext(request) )



@staff_member_required
def edit_user( request, event_name, psdid ):
    try:
        regrec = RegRecord.objects.get(event=event_name, psdid=psdid)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to edit a regrecord that does not exist  Please try again." }, 
                                   context_instance=RequestContext(request)  )
    
    rev = reverse("admin:register_regrecord_change", args=(regrec.id,))
    return HttpResponseRedirect( rev )



@staff_member_required
def edit_event( request, event_name ):
    try:
        event = Event.objects.get(event=event_name)
    except Event.DoesNotExist:
        return render_to_response( 'error.html', {'message' : "Sorry.  You are trying to edit an event that does not exist.  Please try again." }, 
                                   context_instance=RequestContext(request)  )
    
    rev = reverse("admin:register_event_change", args=(event.id,))
    return HttpResponseRedirect( rev )
    
    
    
def make_table_table( event_name, N=100 ):
    TableRecord.objects.filter( event=event_name ).delete()
    cyc = itertools.cycle( (0,1,2,1) )

    for k in range(N,0,-1):
        tb = TableRecord(event=event_name, name="Table " + str(k), quality= cyc.next(), groupOK=False, statOK=False )
        #if k % 4 == 0:
        #    tb.groupOK = True
        #if k % 5 == 0:
        #    tb.statOK = True
            
        #if k in (9,10,11,12,13,1,2,3,4,5,6,7,8):
        #    tb.groupOK = True
        #if k in (1, 2, 3, 4):
        #    tb.statOK = True
            
        tb.save()




def calc_pay_numbers( event_name ):
    totalfolks = 0
    pendings = 0
    paidcount = 0
    cancelled = 0
    comped = 0
    paypal = 0
    refunded = 0
    here = 0
    paidNoShow = 0
    door = 0
    doorString = ""
    regs = RegRecord.objects.filter( event=event_name ).order_by( 'psdid' )
    
    for r in regs:
        totalfolks = totalfolks + r.size
        if r.paid:
            if  "comp" in r.notes:
                comped = comped + r.size
            else:
                paidcount = paidcount + r.size
        if r.pending and not r.cancelled:
            pendings = pendings + r.size
        if r.cancelled:
            cancelled = cancelled + r.size
        if "paypal transation id" in r.notes:
            if r.paid:
                paypal = paypal + r.size
            else:
                refunded = refunded + r.size
        if "refunded" in r.notes and not r.paid:
            refunded = refunded + r.size
        if r.here:
            here = here + r.size
        if r.paid and not r.here:
            paidNoShow = paidNoShow + r.size
        if "door" in r.notes:
            door = door + r.size
            if doorString == "":
                doorString = r.notes
            else:
                doorString += "; " + r.notes
            
    return """
    total = %s  
    paid = %s
    comped = %s       (based on finding "comp" in notes field)
    pending = %s
    cancelled = %s
    paypal = %s    (based on finding "paypal transaction id" in notes field.
    refunded = %s   (based on "paypal transaction id" and no paid flag, or "refunded" in notes field)
    here = %s
    paid no show = %s  (paid, not here)
    door = %s  (door mentioned)
         Remarks are: %s
    """ % ( totalfolks, paidcount, comped, pendings, cancelled, paypal, refunded, here, paidNoShow, door, doorString)
    
    

@staff_member_required
def event_manager( request, event_name, action, extraArg=None ):
    """ 
    This method redirects a bunch of calls to a bunch of different functions.
    It also prints out a list of commands you can do with these calls.
    """

    if action=="" or action=="main":
       
       results="""Click on action desired."""
    elif action=="editevent":
        return edit_event( request, event_name )
    elif action=="listcomments":
        return list_comments(request, event_name, ACTIONS)
    elif action== "listchildcare":
        return list_childcare(request, event_name, ACTIONS)
    elif action=="listpeople":
        list_all = extraArg=="all"
        return list_people(request, event_name, ACTIONS, list_all)
    elif action=="clean":
       results=clean_database()
    elif action=="fix":
       results=fix_group_codes()
    elif action=="matrix":
        updateProc = updateMatchRecords_async(event_name)
        return HttpStreamTextResponse( updateProc, event_name, ACTIONS=ACTIONS )
    elif action=="email":
        results = generate_email_list(request,event_name,ACTIONS)
        return results
    elif action=="countdates":
        resp = HttpResponse(psd.register.demographics.date_distribution_iter(event_name), mimetype='text/plain')
        resp['Content-Disposition'] = 'attachment; filename=datecounts.csv'
        return resp
    elif action=="schedules":
        if extraArg==None:
            extraArg="NotNo"
        #results = 'Make with psdmanage.py command.  Then go to [root url to django]/schedules/%s to have schedules rendered.' % (event_name, ) 
        return HttpResponseRedirect( reverse( "print-schedules", kwargs={'event_name':event_name,'include_code':extraArg} ) )
    elif action=="demographics":
        if extraArg==None:
            extraArg="NotNo"        
        return HttpStreamTextResponse( print_demographics_async( event_name, who_print=extraArg ), event_name, head_string="<p>Demographics for %s with '%s' included</p>" % (event_name, extraArg) )
    elif action=="nametags":
        return HttpStreamTextResponse( make_nametags_async( event_name ), event_name, head_string="<p>Demographics for %s with '%s' included</p>" % (event_name, extraArg) )
    elif action=="plandates":
        return schedule_form( request, event_name )
    elif action=="maketabletable":
        if extraArg == None or extraArg=="":
            extraArg = 100
        else:
              try:
                    extraArg = int(extraArg)
              except:
                    return render_to_response( 'error.html', {'message' : "Sorry.  You need an interger for the argument to maketabletable."},
                                   context_instance=RequestContext(request) )

        make_table_table(event_name,N=extraArg)
        results = "Table file built with %s tables\n" % (extraArg,)
    elif action=="listmissingsheets":
        return HttpStreamTextResponse( list_missing_datesheets_iter( event_name ), event_name, ACTIONS=ACTIONS)
    elif action=="listbadcruises":
        return HttpStreamTextResponse( list_bad_cruises_iter( event_name ), event_name, ACTIONS=ACTIONS )
    elif action=="emailpostevent":
        return HttpResponseRedirect( reverse( "email-post-event" ) )
    elif action=="emailpreevent":
        return HttpResponseRedirect( reverse( "email-pre-event", kwargs={'event_name':event_name} ) )
    elif action=="checkin":
        return HttpResponseRedirect( reverse( "check-in", kwargs={'event_name':event_name} ) )
    elif action=="installRpackages":
        return HttpStreamTextResponse( install_packages_async(), event_name, head_string="<p>Attempting to download and install R packages</p>" )
    elif action=="testR":
        return HttpStreamTextResponse( test_R_async(), event_name, head_string="<p>Testing R Code</p>" )
    elif action=="calcpay":
        return HttpStreamTextResponse( calc_pay_numbers(event_name), event_name, head_string="<p>Computing Pay Statistics</p>" )
    elif action=="datesheet":
        return HttpResponseRedirect( reverse( "next-date-sheet", kwargs={'event_name':event_name} ) )
    else:
       results="Action '%s' not understood." % (action,)
    
    if not results == """Click on action desired.""":
        results = "<b>RESULT:</b><p>" + results
        
    return render_to_response( 'event_manager.html', {'event_name':event_name,
                                   'actions':ACTIONS,
                                   'results':results } )
    


